$(function(){
	var pageSize = getPageSize();
	var temp_option = {temp:[]};
	$('#chart').css({
		width:pageSize[2]+'px',
		height:pageSize[3]+'px'
	})
	var $scope = {data:{
 		fieldDate: '',
 		chart_x: [],
 		chart_y: [],
 		chartType: [],
 		xData: [],
 		mychart: '',
 		pieseries: [],
 		lineseries: [],
 		barseries: [],
 		legendData: [],
 		searchData: [],
 		searchList: []
 	}};
	$scope.class22 = {
		chart_type: chart_type,
		show_edit: false
	};
 	$scope.initEcharts = function(){
		$scope.data.mychart = echarts.init(document.getElementById("chart"),'macarons');
		if($scope.class22.chart_type == 'line'){
			option = {
				tooltip: {
			        trigger: 'axis',
			        axisPointer :{
			        	type:"shadow"
			        }
			    },
				legend: {
					top: '5%',
			        data: $scope.data.legendData
			    },
			    xAxis: {
			        type: 'category',
			        boundaryGap: false,
			        data: $scope.data.xData
			    },
			    yAxis: {
			        type: 'value'
			    },
			    series:$scope.data.lineseries
			};
			temp_option.temp = [option];
			$scope.data.mychart.setOption(option);		
		}else if($scope.class22.chart_type == 'bar'){
			option = {
				tooltip: {
			        trigger: 'axis',
			        axisPointer :{
			        	type:"shadow"
			        }
			    },
				legend: {
					top: '5%',
			        data: $scope.data.legendData
			    },
			    xAxis: {
			        type: 'category',
			        data: $scope.data.xData,
			        axisTick: {
		                alignWithLabel: true
		            }
			    },
			    yAxis: {
			        type: 'value'
			    },
			    series:$scope.data.barseries
			};
			temp_option.temp = [option];
			$scope.data.mychart.setOption(option);
		}else if($scope.class22.chart_type == 'pie'){
			option = {
			    legend: {
			        orient: 'vertical',
			        itemWidth: 5,
			        itemHeight: 5,
			        x: 'right',
			        align: 'left',
			        data: $scope.data.xData
			    },
			    series: $scope.data.pieseries
			};
			temp_option.temp = [option];
			$scope.data.mychart.setOption(option);
		}
	}
 	
 	var vd = [];
 	$.each(chart_x, function(iii,eee) {
 		vd.push(eee.fieldName)
 	})
 	vd = JSON.stringify(vd);
 	
 	//重新取值
	$.each(chart_x, function(ii,e) {
		getResult(e.viewName,e.fieldName,vd,function(bk){
			chart_x[ii].data = bk;
			$.each(chart_y, function(jj,f) {
				getResult(f.viewName,f.fieldName,vd,function(bk2){
					chart_y[jj].data = bk2;
					$scope.data.chart_x = chart_x;
					$scope.data.chart_y = chart_y;
					if($scope.data.chart_x[0]){
						$scope.data.xData = $scope.data.chart_x[0].data;
					}
					if($scope.data.lineseries){
						$scope.data.lineseries = [];
					}
					if($scope.data.barseries){
						$scope.data.barseries = [];
					}
					if($scope.data.pieseries){
						$scope.data.pieseries = [];
					}
					for (var i = 0; i < $scope.data.chart_y.length; i++) {
						$scope.data.legendData.push($scope.data.chart_y[i].title);
						$scope.data.lineseries.push({
							name: $scope.data.chart_y[i].title,
				            type: 'line',
				            data: $scope.data.chart_y[i].data
						});
						
						$scope.data.barseries.push({
							name: $scope.data.chart_y[i].title,
				            type: 'bar',
				            data: $scope.data.chart_y[i].data
						});
						
						var userName = [], userValue = [] ,pieData = [];
							userName = $scope.data.chart_x[0].data;
							userValue = $scope.data.chart_y[i].data;
						for(var j = 0; j< userName.length; j++){
							pieData.push({
								value: userValue[j],
								name: userName[j]
							});
						}
						$scope.data.pieseries.push({
							name: $scope.data.chart_y[i].title,
				            type: 'pie',
				            radius: ['50%', '70%'],
				            label: {
				                normal: {
				                    show: false,
				                    position: 'center'
				                },
				                emphasis: {
				                    show: true,
				                    textStyle: {
				                        fontSize: '30',
				                        fontWeight: 'bold'
				                    }
				                }
				            },
				            data: pieData
						});
						
					}
					
					if($scope.data.chart_x != '' && $scope.data.chart_y != ''){
						$scope.initEcharts();
					}
				})
			});
		})
	});
})